﻿using Enum;
using MediatR;
using Practice.Common.Domains;
using Practice.Common.Dto;
using Practice.Common.Infrastructure.Interfaces;

namespace Practice.API.Application.Commands.Roles
{
    public class RoleListHandler : IRequestHandler<RoleListCommand, Result>
    {
        public readonly IBaseRepository<Role> RoleRepository;
        public readonly IBaseRepository<RolePermission> RolePermissionRepository;
        public readonly IBaseRepository<Permission> PermissionRepository;

        public RoleListHandler(IBaseRepository<Role> roleRepository, IBaseRepository<RolePermission> rolePermissionRepository, IBaseRepository<Permission> permissionRepository)
        {
            RoleRepository = roleRepository;
            RolePermissionRepository = rolePermissionRepository;
            PermissionRepository = permissionRepository;
        }

        public async Task<Result> Handle(RoleListCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.Success };
            var res = new PageResult() { };
            try
            {
                var list = (from a in RoleRepository.GetAll()
                            join b in RolePermissionRepository.GetAll() on a.RoleId equals b.RoleId
                            join c in PermissionRepository.GetAll() on b.PermissionId equals c.PermissionId
                            where a.RoleStatus == false
                            group c by new {a.RoleId,a.RoleName,b.CreateTime,a.RoleStatus} into g
                            select new RolePermissionDto()
                            {
                                RoleId = g.Key.RoleId,
                                RoleName = g.Key.RoleName,
                                PermissionName = string.Join(',',g.Select(x=>x.PermissionName)),
                                RoleStatus = g.Key.RoleStatus,
                                CreateTime = g.Key.CreateTime
                            }).ToList(); ;


                res.TotalCount = list.Count;
                res.PageCount = (int)Math.Ceiling(list.Count * 1.0 / request.pagesize);
                if (request.RoleName != null)
                {
                    list = list.Where(x => x.RoleName.Contains(request.RoleName)).ToList();
                }
                res.Data = list.OrderBy(x => x.RoleId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();


                result.Data = res;
                result.Code = (int)CommonEnumCode.Success;



            }
            catch (Exception)
            {

                result.Msg = "显示查询失败";

            }
            return result;
        }
    }
    public class PageResult
    {
        public int PageCount { get; set; }
        public int TotalCount { get; set; }
        public List<RolePermissionDto> Data { get; set; }
    }
    public class RolePermissionDto
    {
        public int RoleId { get; set; }
        public string RoleName { get; set; }
        public bool RoleStatus { get; set; }
        public string PermissionName { get; set; }
        public int PermissionId { get; set; }
        public string CreateTime { get; set; } = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

    }

}
